package com.breakfast.fun.ui.user.address;

import com.breakfast.fun.data.entity.user.address.Address;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by hp on 2016/9/8.
 */
public class UserAddressPresenter extends UserAddressContract.Presenter {
    @Override
    public void onStart() {
        getAddress();
    }

    @Override
    public void getAddress() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "list");

        showLoadingDialog(true);
        rxManager.add(model.getAddressModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Address>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Address address) {
                        hideLoadingDialog();
                        if (address.isSuccess()) {
                            view.showAddress(address);
                        } else {
                            T.show(address.getMsg());
                        }
                    }

                }));
    }
}
